<html>
	<head>
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

		  <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-162001686-1"></script>


	</head>
<script>

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-162001686-1');


function ValidateIPaddress(ipaddress) {
  //if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
  if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
    return (true)
  }
  alert("You have entered an invalid network prefix!")
  return (false)
}

function scan() {
	var ports = [8266, 8261];
	var found=0;
	var count=0;
	var last=255;
	net = document.getElementById("network").value;
	if (!ValidateIPaddress(net))
		return;

	for (var i = 1; i <= last; i++) {

		net = document.getElementById("network").value;
		x = "ws://" + net + "." + i + ":" + ports[0];
		//x = "ws://192.168.0." + i + ":" + ports[0];
		console.log("Trying  " + x);
		var s = new WebSocket(x);
		s.start = performance.now();
	s.port = ports[0];

	s.onerror = function() {
		count++;
		t=this.url.split(":")[1];
		i=t.split(".")[3];

		var div = document.getElementById('resultE');
		div.innerHTML += this.url + '<br>';

		var div = document.getElementById('wait');
		div.innerHTML = "Status: " + count + "/" + last + " IPs scanned" + '<br>';



		console.log("Error IP (i=" + i + ", last=" + last + ",c=" + count + "): " + this.url + " Port " + this.port + ": " + (performance.now() - this.start) + " ms"); 

		if (count==last) {
			var div = document.getElementById('wait');
			div.innerHTML += 'Scan finished.<br>';
		}
	};

	s.onopen = function() {
		found++;
		count++;
		t=this.url.split(":")[1];
		i=t.split(".")[3];
		var div = document.getElementById('result');
		div.innerHTML += "<a href=#>" + this.url + '</a>';

		div.innerHTML += ' <input type="button" value="Select" onclick="SetName(\'' + this.url + '\');" /><br>';



		var div = document.getElementById('wait');
		div.innerHTML = "Status: " + count + "/" + last + " IPs scanned" + '<br>';



		console.log("Success IP (i=" + i + ", last=" + last + ",c=" + count+"): " + this.url + " Port " + this.port + ": " + (performance.now() - this.start) + " ms"); 


		if (count==last) {
			var div = document.getElementById('wait');
			div.innerHTML += ' | Finished.<br>';
		}

	};
	}
}
</script>
<body>
	<center>
	<p> BIPES WebREPL Scanner</p>
	</center>
	Network prefix: <input type="text" id="network" name="network" value="192.168.0"> <button onclick="scan()">Start scan</button>

	<p> <div id="wait">Please, select the network prefix, and click Start Scan.</div></p>
<br>
<br>
Found:
<br>
 <div id="result"></div>
 <br><br>
Not found:
<br>
 <div id="resultE"></div>



<br>
<script type="text/javascript">
    function SetName(pIn) {
        if (window.opener != null && !window.opener.closed) {
            var parLong = window.opener.document.getElementById("url");
            parLong.value = pIn;
        }
        window.close();
    }
</script>

</body>

</html>
